package utils;


public class Path {
	public Node root;
	public Node leaf;
	public Node corespondingNode;

	public Path(Node root, Node leaf, Node corespondingNode) {
		this.root = root;
		this.leaf = leaf;
		this.corespondingNode = corespondingNode;
	}

	@Override
	public String toString() {
		return leaf + " ---> " + root;
	}

	public String getFullPath() {
		String out = leaf + "->";
		Node parent = leaf.parent;
		while (parent.ID != root.ID) {
			out += parent + "->";
			parent = parent.parent;
		}
		out += root;

		return out;
	}
	
	// returns only the path
	// this is because we keep references to the nodes in the tree
	public Path getPath()
	{
		Node newleaf = new Node(this.leaf.ID, this.leaf.label, null, null);
		Node coresp = null;
		try {
			coresp = (Node)this.corespondingNode.clone();
		} catch (CloneNotSupportedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Node iterator = this.leaf.parent;
		Node child = newleaf;
		boolean found = false;
		Node newroot = null;
		
		while(!found)
		{
			if (iterator.ID == this.root.ID)
				found = true;
			
			newroot = new Node(iterator.ID, iterator.label, null, null);
			child.parent = newroot;
			newroot.children.add(child);
			
			child = newroot;
			iterator = iterator.parent;
		}
		
		return new Path(newroot, newleaf, coresp);
	}
	
}
